package com.wanmait.wanmarecruit.manageController;

import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.entity.Manager;
import com.wanmait.wanmarecruit.enumeration.ErrorEnum;
import com.wanmait.wanmarecruit.service.ConfigService;
import com.wanmait.wanmarecruit.service.ManagerService;
import com.wanmait.wanmarecruit.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("manage")
public class ManageManagerController {

    @Resource
    private ManagerService managerService;
    @Resource
    private ConfigService configService;

    @RequestMapping("getAllManagers")
    public AjaxResult getAllManagers(Integer pageNum)
    {
        Integer pageSize = Integer.parseInt(configService.getByKeyword("manager_list_paging_size").getConfigValue());
        PageInfo<Manager> managers = managerService.findAllManagerByPage(pageNum,pageSize);
        return AjaxResult.success("查询成功").put("managers",managers);
    }

    @GetMapping("getOneManagerInfo")
    public AjaxResult getOneManagerInfo(Integer id)
    {
        Manager manager = managerService.selectOneById(id);
        return AjaxResult.success("查询成功").put("manager",manager);
    }

    @PostMapping("updateOneManager")
    public AjaxResult updateOneManager(@RequestBody Manager manager)
    {
        managerService.updateById(manager);
        return AjaxResult.success("保存成功");
    }

    @PostMapping("insertOneManager")
    public AjaxResult insertOneManager(@RequestBody Manager manager)
    {
        managerService.insertOneManager(manager);
        return AjaxResult.success("保存成功");
    }

    @GetMapping("checkName")
    public AjaxResult checkName(String name)
    {
        if (!managerService.checkNameIsExist(name))
        {
            return AjaxResult.error(ErrorEnum.NAME_EXIST.getCode(), ErrorEnum.NAME_EXIST.getInfo());
        }
        return AjaxResult.success("查无此人");
    }

    @GetMapping("deleteOneManager")
    public AjaxResult deleteOneManager(Integer id)
    {
        managerService.deleteOneManager(id);
        return AjaxResult.success("删除成功");
    }
}
